<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;
use app\common\model\Goods;

class GoodsCate extends Model
{

	//关联子节点
	public function childs(){
        return $this->hasMany('goods_cate','pid');
	}
	
	//关联商品
	public function goods(){
        return $this->hasMany('goods','cate_id');
	}
	
	/**
     * 获取列表
     * @param type $shopId
     * @return type
     */
	public function getList($shopId,$type=0)
    {
    	$model = self::alias('a');
		if($shopId>0){
			$model = $model->where(function ($query) use($shopId) {
				$query = $query->where('a.shop_id', 0);
			    $query = $query->whereOr('a.shop_id','');
			    $query = $query->whereOr('a.shop_id',$shopId);
			});
		}else{
			$model = $model->where("a.shop_id",$shopId);
		}
		$model = $model->where("a.is_show",1);
		$model = $model->where("a.is_del",0);
		$model = $model->where("a.pid",0);
		$model = $model->field("a.id,a.name");
		$model = $model->order("a.sort_no");
		$result = $model->select();
		
		//子分类
		$cates = [];
		foreach($result as $key => $value ){
			$childs = $value->childs()->field("id,name,pid")->select()->toArray();
			$data = [];
			foreach($childs as $k => $val ){
				$model = model('goods')->getModel();
				if($type){
					$model = $model->where('type','in',$type);
				}else{
					//$model = $model->where('type','<>',1);
				}
				$model = $model->where('a.cate_id',$val['id']);
				$model = $model->where('a.name','like',"%".$name."%");
				$val['goods_count'] = count( $model->select() );
				if( $val['goods_count']>0 ){
					$data[] = $val;
				}
			}
			if($data){
				$value['childs'] = $data;
				$cates[] = $value;
			}
		}
		//返回
    	return $cates;
    }
	
	/**
     * 获取系统分类列表
     * @return type
     */
	public function getSystemList()
    {
    	$model = new self;
		$model = $model->where("is_show",1);
		$model = $model->where("shop_id",0);
		$model = $model->where("is_del",0);
		$model = $model->field("id,name");
		$model = $model->order("sort_no",'asc');
		$model = $model->order("add_time",'desc');
		return $model->select();
    }
	
	/**
     * 添加分类
     * @param type $data
     * @return type
     */
	public function add($data)
    {
    	$validate = new Validate;
		$validate->rule([
			'shop_id'=>'require',
        	'name' => 'require|length:2,10|unique:goods_cate,name^shop_id',
		]);
		$validate->message ([
			'name.unique'=>'该分类名称已存在！',
		]);
        if( !$validate->check($data) ) {
        	throw new \think\Exception($validate->getError());
            return false;
        }
    	self::allowField(true) -> save($data);
		return $this;
    }
	
	/**
     * 编辑分类
     * @param type $data
     * @return type
     */
	public function edit($data) {
		$validate = new Validate;
		$validate->rule([
			'shop_id'=>'require',
        	'name' => 'require|length:2,10',
		]);
        if( !$validate->check($data) ) {
        	$this -> errorInfo = $validate->getError();
            return false;
        }
		$find = $this->where('name',$data['name'])
			->where('shop_id',$data['shop_id'])
			->where('id','<>',$data['id'])
			->find();
        if( $find  ) {
        	throw new \think\Exception('分类名称已存在');
        }
    	self::allowField(true)->isUpdate(true) -> save($data);
		return $this;
    }
	
	
	/**
     * 删除分类
     * @param type $id
     * @return type
     */
	public function del($shopId,$id)
    {
    	return self::where('shop_id',$shopId)->where('id',$id) ->delete();
    }
	
	
	//更新排序 
	public function updateSort($moveId,$coverId){
		$rerult = $this->saveAll( [
		    ['id'=>$moveId, 'sort_no'=> $this->where('id',$coverId)->value('sort_no') ],
		    ['id'=>$coverId, 'sort_no'=> $this->where('id',$moveId)->value('sort_no') ]
		] );
		return $rerult;
	}
	
}
